package com.leyou.order.controller;

import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import com.leyou.order.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.stream.Collectors;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName OrderController.java
 * @Description TODO 订单控制器
 * @createTime 2020年05月21日 10:45:00
 */

// @Controller
@RestController
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 创建订单接口
     *
     * @param orderDTO      接收请求的参数
     * @param bindingResult 异常校验结果
     * @return 返回订单ID
     */
    @PostMapping("/order")
    public ResponseEntity<Long> createOrder(@Valid @RequestBody OrderDTO orderDTO, BindingResult bindingResult) {
        // 手动从user中取出值来进行判空等校验： 服务器端校验
        if (bindingResult.hasErrors()) {
            // 如果有异常，收集异常信息
            String errors = bindingResult.getAllErrors()
                    .stream()
                    .map(ObjectError::getDefaultMessage)
                    .collect(Collectors.joining("|"));
            throw new LyException(400, errors);
        }
        // 创建订单
        Long orderId = orderService.createOrder(orderDTO);
        // 返回
        return ResponseEntity.ok(orderId);
    }


    /**
     * 订单查询
     *
     * @param id
     * @return
     */
    @GetMapping("/order/{id}")
    public ResponseEntity<OrderVO> findOrderById(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.findOrderById(id);
        return ResponseEntity.ok(orderVO);
    }


    /**
     * 生成支付链接
     *
     * @param id
     * @return
     */
    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long id) {
        String payUrl = orderService.getPayUrl(id);
        return ResponseEntity.ok(payUrl);
    }


    /**
     * 查询订单的状态
     *
     * @param id 订单编号
     * @return 返回订单的状态
     */
    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> findOrderState(@PathVariable("id") Long id) {
        Integer state = orderService.findOrderState(id);
        return ResponseEntity.ok(state);
    }


    /**
     * 根据ThreadLocal记录的用户查询其所有订单
     *
     * @return
     */
    @GetMapping("/order/page")
    public ResponseEntity<PageResult<OrderVO>> findOrdersByPage() {
        PageResult<OrderVO> orders = orderService.findOrdersByPage();
        return ResponseEntity.ok(orders);
    }


    /**
     * 根据传入的status分页查询用户相应的订单(1234枚举)
     *
     * @param status
     * @return
     */
    @GetMapping("/order/page/status/{status}")
    public ResponseEntity<PageResult<OrderVO>> findOrdersByPageByStatus(@PathVariable("status") Integer status) {
        PageResult<OrderVO> orders = orderService.findOrdersByPageByStatus(status);
        return ResponseEntity.ok(orders);
    }


    /**
     * 通过关键字搜索查找订单
     *
     * @param keyWord
     * @return
     */
    @GetMapping("/order/page/keyword/{keyword}")
    public ResponseEntity<PageResult<OrderVO>> findOrdersByKeyWordSearch(@PathVariable("keyword") String keyWord) {
        PageResult<OrderVO> orders = orderService.findOrdersByKeyWordSearch(keyWord);
        return ResponseEntity.ok(orders);
    }

}